﻿// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text.RegularExpressions;
using System.Windows.Input;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Foundation.Metadata;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace SerialCommand
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class CommandCapture : Page
    {
        private ObservableCollection<CommandViewMode> listOfCommand;
        SerialCommRegex serialCommRegex;
        public CommandCapture()
        {
            this.InitializeComponent();
            listOfCommand = new ObservableCollection<CommandViewMode>();
            serialCommRegex = SerialCommRegex.Current;
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            var currentWindow = WindowHelper.GetWindowForElement(this);
            currentWindow.Title = "命令捕获";
            currentWindow.ExtendsContentIntoTitleBar = true;
            AppTitleBar.Height = 30;
            AppTitleBar.MinWidth = 188;
            AppTitleBar.VerticalAlignment = VerticalAlignment.Top;
            currentWindow.SetTitleBar(AppTitleBar);
            CommandList.Source = listOfCommand;
        }
        private CommandViewMode FindCommand(String command)
        {
            if (command != null)
            {
                foreach (CommandViewMode entry in listOfCommand)
                {
                    if (entry.Name == command)
                    {
                        return entry;
                    }
                }
            }

            return null;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string name = CommandName.Text;
            string regex = name + ".*?(" + CommandRegex.Text + ")";
            if (name == String.Empty || regex == String.Empty)
            {
                return;
            }
            var match = FindCommand(name);
            if (match != null)
                return;
            CommandViewMode tRegex = new CommandViewMode(name, regex, 5);
            var deleteCommand = new StandardUICommand(StandardUICommandKind.Delete);
            deleteCommand.ExecuteRequested += DeleteCommand_ExecuteRequested;
            tRegex.Command = deleteCommand;
            listOfCommand.Add(tRegex);
            serialCommRegex.register(name, new ExecComm
            {
                regex = regex,
                action = (string targe, Match match) =>
                {
                    string s = match.Groups[1].Value;
                    MainWindow.Current.invoke(() => { FindCommand(targe).Value = s; });
                    return false;
                },
            });
        }
        private void DeleteCommand_ExecuteRequested(XamlUICommand sender, ExecuteRequestedEventArgs args)
        {
            if (args.Parameter != null)
            {
                foreach (var i in listOfCommand)
                {
                    if (i.Name == (args.Parameter as string))
                    {
                        serialCommRegex.unregister(i.Name);
                        listOfCommand.Remove(i);
                        return;
                    }
                }
            }
        }
    }

    public class CommandViewMode : INotifyPropertyChanged
    {
        private string name;
        private string regex;
        private string value;
        public CommandViewMode(string name, string des, int pri)
        {
            this.name = name; regex = des;
        }
        public string Name { get { return name; } }
        public string Value { get { return value; } set { this.value = value; this.OnPropertyChanged(); } }
        public string Regex { get { return regex; } }

        public ICommand Command { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            // Raise the PropertyChanged event, passing the name of the property whose value has changed.
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
